<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Cache;

class Course extends Model
{
	public function getWhereData($param)
	{
		return self::field('course_id,course_name,course.type_id,course.s_id,course.is_free,type,points,video_time')
						->where('course.is_show', 1)
						->where('type.is_show', 1)
						->where('s_show', 1)
						->when($param['nav_id'], function($query) use ($param){
							$query->where('course.nav_id', $param['nav_id']);
						})
						->when($param['type_id'], function($query) use ($param){
							$query->where('course.type_id', $param['type_id']);
						})
						->when($param['s_id'], function($query) use ($param){
							$query->where('course.s_id', $param['s_id']);
						})
						->join('specific','specific.s_id=course.s_id')
						->join('type','type.type_id=course.type_id')
						->order('course_sort desc,course_id asc')
						->select();
	}

	public function getAllData($param)
	{
		return self::field('course_id,course_name')
						->where('course.is_show', 1)
						->where('type.is_show', 1)
						->where('s_show', 1)
						->when($param['kw'], function($query) use ($param){
							$query->where('course_name', 'like', '%'.$param['kw'].'%');
						})
						->when($param['types'], function($query) use ($param){
							$query->where('type', $param['types']);
						})
						->join('specific','specific.s_id=course.s_id')
						->join('type','type.type_id=course.type_id')
						->join('nav','nav.nav_id=course.nav_id')
						->order('nav_sort desc,nav.nav_id asc,type_sort desc,type.type_id asc,s_sort desc,specific.s_id asc,course_sort desc,course_id asc')
						->paginate(20,false, ['query'=>$param]);
	}

	public function getCourseIds($value, $field, $limit = '')
	{
		return self::where($field, $value)
						->where('is_show', 1)
						->where('s_show', 1)
						->join('specific','specific.s_id=course.s_id')
						->when($limit, function($query) use ($limit){
							$query->limit($limit);
						})
						->order('course_sort desc,course_id asc')
						->column('course_id');
	}

	public function getCoursePoints($navIds)
	{
		return self::where('course.nav_id', 'in', $navIds)
						->where('course.is_show', 1)
						->where('type.is_show', 1)
						->where('s_show', 1)
						->join('specific','specific.s_id=course.s_id')
						->join('type','type.type_id=course.type_id')
						->order('course_sort desc,course_id asc')
						->column('course.type_id,sum(points)');
	}

	public function getDataOne($id)
	{
		return self::field('course.*,s_free,cast_id')
						->where('course.is_show', 1)
						->where('course_id', $id)
						->join('type','type.type_id=course.type_id')
						->join('specific','specific.s_id=course.s_id')
						->find();
	}


	public function getTrainData($param)
	{
		return self::field('course_id,course_name')
						->where('is_show', 1)
						->where('type', 2)
						->when($param['s_id'], function($query) use ($param){
							$query->where('s_id', $param['s_id']);
						})
						->order('course_sort desc,course_id asc')
						->select();
	}
}